package com.mflex.WHSmartWhouseAd.adapter;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.recyclerview.widget.RecyclerView;

import com.mflex.WHSmartWhouseAd.R;
import com.mflex.WHSmartWhouseAd.holder.RecycleViewHolder;
import com.mflex.WHSmartWhouseAd.listener.OnRecyclerItemClickListener;
import com.mflex.WHSmartWhouseAd.model.MenuBean;

import java.util.ArrayList;
import java.util.List;

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecycleViewHolder> implements View.OnClickListener{
    public List<MenuBean> mDatas = new ArrayList<MenuBean>();
    private Context mContext;
    private OnRecyclerItemClickListener listener;
    private RecyclerView rvParent;

    public RecyclerViewAdapter(Context context, List<MenuBean> datas){
        this.mContext = context;
        this.mDatas = datas;
    }

    @Override
    public RecycleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        rvParent=(RecyclerView)parent;
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_main, parent,false);
        view.setOnClickListener(this); //设置监听器
        RecycleViewHolder holder = new RecycleViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(RecycleViewHolder holder, final int position) {
        holder.getTxt_title().setText(mDatas.get(position).getTxt_title());
//        holder.getTxt_info().setText(mDatas.get(position).getTxt_info());
        holder.getImg().setImageResource(mDatas.get(position).getImg_title());
    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    //第二步， 写一个公共的方法
    public void setOnItemClickListener(OnRecyclerItemClickListener listener) {
        this.listener = listener;
    }

    @Override
    public void onClick(View view) {
        int position = rvParent.getChildAdapterPosition(view);
        if(listener !=null){
            listener.onRecyclerItemClick(rvParent,view,position);
        }
    }
}
